Exercise 1
library(ggplot2)
library(tibble)
# Load the mpg dataset
data(mpg)
# Create a scatterplot with customized aesthetics
ggplot(mpg, aes(x = cty, y = hwy, color = manufacturer, size = displ)) +
geom_point(size = 2) +
theme_void() +
theme()
Exercise 2
polar_art <- function(seed, n, palette) {
# set the state of the random number generator
set.seed(seed)
# data frame containing random values for
# aesthetics to use in the art
dat <- tibble(
x0 = runif(n),
y0 = runif(n),
x1 = x0 + runif(n, min = -.2, max = .2),
y1 = y0 + runif(n, min = -.2, max = .2),
shade = runif(n),
size = runif(n)
)
# plot segments in various colours, using
# polar coordinates and a gradient palette
dat |>
ggplot(aes(
x = x0,
y = y0,
xend = x1,
yend = y1,
colour = shade,
size = size
)) +
geom_segment(show.legend = FALSE) +
coord_polar() +
scale_y_continuous(expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
scale_colour_gradientn(colours = palette) +
scale_size(range = c(0, 10)) +
theme_void()
}
# Define a fun palette
fun_palette <- c("#FF4081", "#FFC107", "#8BC34A", "#03A9F4", "#9C27B0")
# Set the seed for reproducibility
seed <- 7829
# Generate a piece using the fun palette
polar_art(seed, n = 200, palette = fun_palette)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
polar_art <- function(seed, n, palette) {
set.seed(seed) # Set the seed for reproducibility
# Generate random coordinates for the polar art
x <- runif(n, min = -1, max = 1)
y <- runif(n, min = -1, max = 1)
# Plot the polar art
plot(x, y,
col = palette,
pch = 19,
xlim = c(-1, 1), ylim = c(-1, 1),
xlab = "", ylab = "",
main = NA,
axes = FALSE)
}
# Save the modified polar_art() function in the "polar_art_02.R" file
file_path <- "polar_art_02.R"
writeLines(deparse(polar_art), file_path)
# Load the modified polar_art() function from the "polar_art_02.R" file
source("polar_art_02.R")
# Generate and display the custom polar art with a random seed and custom palette
seed <- sample(1:1000, 1) # Randomly select a seed from 1 to 1000
polar_art(seed, n = 7000, palette = fun_palette)
We might be naming the function as a second version in an art context
because it we are going for random art, and if we forget which function
does which design in the plot, that just adds to the randomness and
beauty of the function as a whole.
Exercise 3
# Define the sample_canva() function
sample_canva <- function() {
# Generate a sample color palette
palette <- c("#FF4081", "#536DFE", "#FFD740", "#1DE9B6", "#9C27B0")
return(palette)
}
# Generate a color palette using sample_canva()
pal_ette <- sample_canva()
sample_named_colours <- function(n) {
# Get the set of distinct named colours
all_colours <- colours(distinct = TRUE)
# Sample n distinct colours
sampled_colours <- sample(all_colours, size = n)
return(sampled_colours)
}
# Define the polar_art() function
polar_art <- function(seed, n, color_palette) {
set.seed(seed) # Set the seed for reproducibility
# Generate random coordinates for the polar art
x <- runif(n, min = -1, max = 1)
y <- runif(n, min = -1, max = 1)
# Plot the polar art using ggplot2
data <- data.frame(x = x, y = y, color = color_palette)
ggplot(data, aes(x = x, y = y)) +
geom_point(size = 3, shape = 16, aes(color = color)) +
scale_color_identity() +
theme_void()
}
# Set the seed for reproducibility
seed <- 123
# Generate a random palette of 5 named colors
num_colors <- 5
color_palette <- sample_named_colours(num_colors)
# Generate and display the polar art using the random palette
polar_art(seed, n = 500, color_palette = color_palette)
sample_canva_palette <- function(n) {
# Get the set of distinct colors from ggthemes::canva_palettes
all_colors <- unlist(ggthemes::canva_palettes)
# Sample n distinct colors randomly
sampled_colors <- sample(all_colors, size = n)
return(sampled_colors)
}
#Generate a random palette of 5 colors
num_colors <- 5
random_palette <- sample_canva_palette(num_colors)
print(random_palette)
## Modern and muted1 Subdued and proffesional4 Nightlife2
## "#a9b7c0" "#763626" "#ff0038"
## Coastal2 Earthy and fresh1
## "#f3d3b8" "#945d60"
Exercise 3
#Copying functions due to R not cooperating
sample_data <- function(seed = NULL, n = 100){
if(!is.null(seed)) set.seed(seed)
dat <- tibble(
x0 = runif(n),
y0 = runif(n),
x1 = x0 + runif(n, min = -.2, max = .2),
y1 = y0 + runif(n, min = -.2, max = .2),
shade = runif(n),
size = runif(n),
shape = factor(sample(0:22, size = n, replace = TRUE))
)
}
polar_styled_plot <- function(data = NULL, palette) {
ggplot(
data = data,
mapping = aes(
x = x0,
y = y0,
xend = x1,
yend = y1,
colour = shade,
size = size
)) +
coord_polar(clip = "off") +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, 1),
oob = scales::oob_keep
) +
scale_x_continuous(
expand = c(0, 0),
limits = c(0, 1),
oob = scales::oob_keep
) +
scale_colour_gradientn(colours = palette) +
scale_size(range = c(0, 10)) +
theme_void() +
guides(
colour = guide_none(),
size = guide_none(),
fill = guide_none(),
shape = guide_none()
)
}
library(ggplot2)
# Generate random data
set.seed(123)
data <- sample_data(seed = 123, n = 200)
# Visualization using ggplot2
ggplot(data, aes(x = x0, y = y0, size = size, fill = shade)) +
geom_point(shape = 21, color = "black") +
scale_size_continuous(range = c(1, 10)) +
scale_fill_gradient(low = "blue", high = "red") +
coord_polar(theta = "x", start = 0) +
labs(title = "", x = "", y = "") +
theme_void()+
theme(legend.position = "none")
Task 2 Doing it for spatial tricks Loading in packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(purrr)
library(tibble)
library(ggplot2)
library(ggthemes)
library(ambient)
# Define canvas
x_coords <- seq(from = 0, to = 1, length.out = 800)
y_coords <- seq(from = 0, to = 1, length.out = 800)
canvas <- long_grid(x = x_coords, y = y_coords)
# Generate artwork using gen_perlin() with different values
set.seed(1234)
# Default gen_perlin() output
canvas <- canvas %>%
mutate(paint = gen_perlin(x, y, frequency = 10, seed = 1234))
art_default <- ggplot(canvas, aes(x, y, fill = paint)) +
geom_raster(show.legend = FALSE)
# Altered gen_perlin() output with different frequency
canvas <- canvas %>%
mutate(paint = gen_perlin(x, y, frequency = 1, seed = 1234))
art_frequency <- ggplot(canvas, aes(x, y, fill = paint)) +
geom_raster(show.legend = FALSE)
# Altered gen_perlin() output with different seed
canvas <- canvas %>%
mutate(paint = gen_perlin(x, y, frequency = 10, seed = 5678))
art_seed <- ggplot(canvas, aes(x, y, fill = paint)) +
geom_raster(show.legend = FALSE)
# Plot the artworks
art_default
art_frequency
art_seed
make_noise_art <- function(
generator = gen_perlin,
frequency = 10,
seed = 1234,
pixels = 2000,
palette = c("#e5ddc8", "#01949a", "#004369", "#db1f48"),
...
) {
# define the grid
canvas <- long_grid(
x = seq(from = 0, to = 1, length.out = pixels),
y = seq(from = 0, to = 1, length.out = pixels)
)
# use the generator to add paint
canvas <- canvas |>
mutate(
paint = generator(
x, y,
frequency = frequency,
seed = seed,
...
)
)
# use ggplot2 to draw the picture
art <- canvas |>
ggplot(aes(x, y, fill = paint)) +
geom_raster(show.legend = FALSE) +
theme_void() +
coord_equal() +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_gradientn(colours = palette)
return(art)
}
# Generate noise art with default arguments
make_noise_art()
# Vary the seed
make_noise_art(seed = 123)
make_noise_art(seed = 568)
# Vary the frequency
make_noise_art(frequency = 10)
make_noise_art(frequency = 20)
# Vary the palette
make_noise_art(palette = c("white", "black"))
# Vary the generator
make_noise_art(generator = gen_perlin)